;
(function() {
	var skill = {
		//进货
		//需要拓展的细节包括：自动归类到各种分支产品中
		importGoods: function(goods_info, num, no_store) {
			var self = this;
			if (!no_store) {
				if (self.store.length >= this.getMaxGoodsNum()) {
					throwCusError("商家的商品达到了上限")
				}
				var goods = new GoodsCon(goods_info, self._id, num || 0);
				self.store.unshift(goods._id);
			} else {
				var goods = new GoodsCon(goods_info, self._id, num || 0);
			}
			self.save();
			console.log("添加货物库存：", goods_info.name, num, "件");
			//结合标签
			goods_info.tags && this.updateGoodsTags(goods._id, goods_info.tags);
			return goods._id;
		},
		updateGoodsInfo: function(goods_info) {
			var self = this;
			var goods = this._getGoods(goods_info._id);
			goods._setInfo(goods_info);
			//结合标签
			goods_info.tags && this.updateGoodsTags(goods._id, goods_info.tags);
			return goods._id;
		},
		//删除商品
		//和商品相关的归类，在获取商品是动态删除与的商品关联
		removeGoods: function(goods_id) {
			var goods = this._getGoods(goods_id, true);
			goods && goods.destroy();
			console.log(this.store.length, goods_id);
			//Gaubee
			this.store = $Array.remove(this.store, goods_id);
			console.log(this.store.length);
			this.save();
		},
		_filter_un_sold_goods: function(goods_id_list) {
			var un_sold_goods = this._getUnSoldGoods();
			goods_id_list = goods_id_list.filter(function(goods_id) {
				return un_sold_goods.indexOf(goods_id) == -1
			});
			return goods_id_list
		},
		//获取商品列表
		getGoodsList: function(num, page, options) {
			var self = this;
			var store = this.store || (this.store = []);
			if (!(options && options.show_un_sold_goods)) {
				//过滤掉下架的商品
				store = this._filter_un_sold_goods(store);
			}
			var res_store = [];
			if (options && options.sort_by_key) { //如果有sort-key，就要全部取出来进行排序再做分页
				GoodsCon.mulGet(store).forEach(function(goods) {
					if (goods) {
						res_store.push(goods);
					} else {
						self.removeGoods(goods_id);
					}
				});
				res_store = db._sortBy(res_store, options.sort_by_key, options.asc)
				res_store = data_cut.page(res_store, num, page);
			} else {
				store = data_cut.page(store, num, page);
				GoodsCon.mulGet(store).forEach(function(goods) {
					if (goods) {
						res_store.push(goods);
					} else {
						self.removeGoods(goods_id);
					}
				});
			}
			return res_store;
		},
		getGoodsListTotalNum: function(options) {
			var store = this.store || (this.store = []);
			if (!(options && options.show_un_sold_goods)) {
				//过滤掉下架的商品
				store = this._filter_un_sold_goods(store);
			}
			return store.length;
		},
		getGoodsList_PageInfo: function(num, page, options) {
			var result = this.getGoodsList(num, page, options);
			var total_num = this.getGoodsListTotalNum(options);
			return {
				total_num: total_num,
				total_page: Math.round(total_num / num) || 1, //至少一页
				goods: result,
				page: +page || 0,
				num: +num || 0
			}
		},
		getGoodsList_PageInfo_EndPayCbUrl: function(num, page, options) {
			var result = this.getGoodsList_PageInfo(num, page, options);
			var self = this;
			result.goods = result.goods.map(function(goods) {
				goods = $Object.clone(goods);
				goods.cb_url_buff = self.getGoodsEndPayCallbackBuff(goods._id);
				return goods;
			});
			return result;
		},
		getGoodsList_IdList: function(goods_id_list, options) {
			var goods_list = [];
			if (Array.isArray(goods_id_list)) {
				if (!(options && options.show_un_sold_goods)) {
					//过滤掉下架的商品
					goods_id_list = this._filter_un_sold_goods(goods_id_list);
				}
				var bus_id = this._id;
				GoodsCon.mulGet(goods_id_list).forEach(function(goods) {
					if (goods && goods.bus_id == bus_id) {
						goods_list.push(goods);
					}
				});
			}
			return goods_list;
		},
		getRandomGoodsList: function(num, options) {
			var self = this;
			var store = (this.store || (this.store = [])).slice();
			if (!(options && options.show_un_sold_goods)) {
				//过滤掉下架的商品
				store = this._filter_un_sold_goods(store);
			}
			num = parseInt(num);
			if (!num) {
				throwCusError("请求商品数量不可为空")
			}
			var res = [];
			for (var i = 0; i < num; i += 1) {
				res = res.concat(store.splice(parseInt(Math.random() * store.length), 1));
			}
			var res_store = [];
			GoodsCon.mulGet(res).forEach(function(goods) {
				res_store.push(goods);
			});
			return res_store;
		},
		//获取精品商品列表
		getQGoodsList: function(num, page, options) {
			var store = this.getGoodsList(null, null, options);
			store = store.sort(function(a_goods, b_goods) {
				//增序列
				return b_goods.getSaleNum() - a_goods.getSaleNum();
			});
			return data_cut.page(store, num, page);
		},
		//通过商品名字获取商品
		getGoodsByName: function(goods_name) {
			var store = this.store || (this.store = []);
			var result;
			store.every(function(current_goods_id) {
				var goods = GoodsCon.get(current_goods_id);
				if (goods.goods_name === goods_name) {
					result = goods;
					return false
				}
				return true;
			});
			return result;
		},
		searchGoods: function(key, options) {
			var goods_list = this.getGoodsList(null, null, options);
			var result = [];
			goods_list.forEach(function(goods) {
				var w = helper.search(goods.goods_name + " " + goods.intro, key);
				if (w) {
					result.push({
						weight: w,
						goods: goods
					});
				}
			});
			result.sort(function(a, b) {
				return b.weight - a.weight;
			});
			return result;
		},
		searchGoods_PageInfo: function(key, num, page, options) {
			var result = this.searchGoods(key, options);
			return {
				total_num: result.length,
				total_page: Math.round(result.length / num) || 1, //至少一页
				goods_with_weight: data_cut.page(result, num, page),
				page: +page || 0,
				num: +num || 0
			}
		},
		//代码片段，根据id获取商品
		_getGoods: function(goods_id, is_silence) {
			var goods = GoodsCon.get(goods_id);
			if (!goods) {
				if (is_silence) {
					return;
				}
				throwCusError("找不到指定商品")
			}
			//因为商品现在不仅仅存在于商家现有仓库store中，还可能存在于会员专区中，所以这里只判断bus_id
			if (goods.bus_id !== this._id) {
				if (is_silence) {
					return;
				}
				throwCusError("商品不属于指定商家")
			}
			return goods;
		},
		//改变商品的位置
		updateGoodsIndex: function(goods_id, goods_index) {
			var store = this.store || (this.store = []);
			var index = store.indexOf(goods_id);
			if (index === -1) {
				throwCusError("商品不属于指定商家")
			}
			store.splice(index, 1);
			goods_index = ~~goods_index;
			if (goods_index > store.length) {
				store.push(goods_id);
			} else if (goods_index < 0) {
				store.unshift(goods_id);
			} else {
				store.splice(goods_index, 0, goods_id)
			}
			this.store = store;
			this.save();
		},
		hasGoodsById: function(goods_id) {
			var store = this.store || [];
			return store.indexOf(goods_id) !== -1;
		},
		//下架商品只是一个ID集合列表
		_getUnSoldGoods: function() {
			var un_sold_goods = this.un_sold_goods;
			if (!Array.isArray(un_sold_goods)) {
				un_sold_goods = this.un_sold_goods = [];
				this.save();
			}
			return un_sold_goods;
		},
		_getUnSoldGoods_byId: function(goods_id) {
			var un_sold_goods = this._getUnSoldGoods();
			if (un_sold_goods.indexOf(goods_id) == -1) {
				throwCusError("商品不属于指定商家下架区");
			}
			return this._getGoods(goods_id);
		},
		// getUnSoldGoodsList: function(num, page) {
		// 	var un_sold_goods = this._getUnSoldGoods();
		// 	un_sold_goods = data_cut.page(un_sold_goods, num, page);
		// 	GoodsCon.mulGet(store).forEach(function(goods) {
		// 		if (goods) {
		// 			res_store.push(goods);
		// 		} else {
		// 			self.removeGoods(goods_id);
		// 		}
		// 	});
		// },
		// getUnSoldGoodsList_PageInfo: function(num, page) {
		// 	var total_num = this._getUnSoldGoods().length;
		// 	var result = this.getUnSoldGoodsList(num, page);
		// 	return {
		// 		total_num: total_num,
		// 		total_page: Math.round(total_num / num) || 1, //至少一页
		// 		goods: result,
		// 		page: +page || 0,
		// 		num: +num || 0
		// 	}
		// },
		// //直接删除商品
		// removeGoodsFromUnSoldGoods: function(goods_id) {
		// 	var goods = this._getUnSoldGoods_byId(goods_id);
		// 	goods && goods.destroy();
		// 	this.un_sold_goods = $Array.remove(this.un_sold_goods || [], goods_id);
		// 	this.save();
		// },
		//商品重新上架，删除下架标识
		removeGoodsFrom_UnSoldGoods: function(goods_id) {
			var un_sold_goods = this._getUnSoldGoods();
			if (un_sold_goods.indexOf(goods_id) == -1) {
				throwCusError("商品不属于指定商家下架区");
			}
			this.un_sold_goods = $Array.remove(un_sold_goods, goods_id);
			this.save();
		},
		//商品下架
		addGoodsTo_UnSoldGoods: function(goods_id) {
			var goods = this._getGoods(goods_id);
			var un_sold_goods = this._getUnSoldGoods();
			if (un_sold_goods.indexOf(goods_id) === -1) {
				un_sold_goods.push(goods_id);
				this.save();
			} else {
				throwCusError("商品已经在下架区");
			}
		},
	};
	module.exports = skill;
	var db = require("../db/index");
	var DomainCon = require("../domain/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
	var tld = require('tldjs');
	var fs = require("fs");
	var Fiber = require('fibers');
}());